package com.suning.service.impl;

import com.suning.entity.Blog;
import com.suning.service.BlogService;
import com.suning.service.ElasticSearchService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 苏宁软件技术有限公司 版权所有
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private ElasticSearchService elasticSearchService;

    /**
     * 根据 tag查询分页结果。
     *
     * @param tag         关键词
     * @param currentPage 当前页
     * @param size        共多少条
     * @return Blog
     */
    @Override
    public List<Blog> query(String tag, long currentPage, long size)  throws Exception {
        Map params = new HashMap();
        params.put("tag", tag);
        params.put("pageSize",size);
        params.put("currentPage",currentPage);
        //List<Blog> result = elasticSearchService.queryForList("blog.queryBlogByTag", Blog.class, params);
        List<Blog> result = elasticSearchService.queryForPage(!"".equals(tag)?"blog.queryBlogByTag":"blog.queryAllBlog", Blog.class, params);


       /* for (Blog blog : result) {
            System.out.println(blog.getHighlight());
        }*/
        return result;
        //return elasticSearchService.queryForPage("user.queryUserByPage", Blog.class, params);
    }

    /**
     * 根据文章标题，返回文章的详细情况。
     *
     * @param id
     * @return
     */
    @Override
    public Blog more(long id)  throws Exception {
        Map params = new HashMap();
        params.put("id", id);
        List<Blog> blogList = elasticSearchService.queryForList("blog.queryMoreBlogById", Blog.class, params);
        if(CollectionUtils.isNotEmpty(blogList))return blogList.get(0);
        else {
            return null;
        }
    }
}
